﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zor_IDAL;
using Zor_Modes;
using Zor_Tools;

namespace Zor_DAL
{
    public class Sys_Navigations_DAL<T> where T : class
    {

        /// <summary>
        /// 查询菜单层级关系
        /// create zhl by 17-3-26
        /// </summary>
        /// <returns></returns>
        ///      
        public static List<T> SelectNavTree()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("WITH cte AS (select Nav_Id,Nav_Title,Nav_ParentID,0 as Lev,cast(Nav_Id as varbinary) as Dt_Sort ");
            sb.Append("from Sys_Navigations where Nav_ParentID=0 UNION ALL SELECT dt.Nav_Id,dt.Nav_Title,dt.Nav_ParentID,Nav_Level+1,cast(c.Dt_Sort+cast(dt.Nav_Id as varbinary) as varbinary) as Dt_Sort   ");
            sb.Append("from cte as c JOIN Sys_Navigations as dt on dt.Nav_ParentID=c.Nav_Id)select *,REPLACE((SPACE(Lev *2)+ Nav_Title),' ','.') AS LevName from cte order by Dt_Sort   ");
            return DapperQuery<T>.DoQueryList(sb.ToString(), null);

        }
        /// <summary>
        /// 根据Id查询数据
        /// create zhl by 2017.3.24
        /// </summary>
        /// <returns></returns>
        public static  T SelectPageListId(string nav_Id)
        {
            var sql = "select * from [V_Sys_Navigations] where Nav_Id =@Nav_Id";
            return DapperQuery<T>.DoQueryT(sql, new { Nav_Id = nav_Id });
        }
        /// <summary>
        /// 菜单管理-新增
        /// create zhl by 2017.3.24
        /// </summary>
        /// <param name="navigation">菜单对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
        public static BaseAnswer InsertNavigations(Sys_Navigations navigation)
        {

            var sql = @"if not exists(select Nav_Title from [Sys_Navigations] where Nav_Title  =@Nav_Title ) 
                        insert into Sys_Navigations values 
                        (@Nav_Title,@Nav_ParentID,@Nav_NavTag,@Nav_Icon,@Nav_Linkurl,@Nav_IsVisible,@Nav_Sortnum,@Nav_Level)";
            BaseAnswer bar = DapperInsert<T>.DoInsert(sql, navigation);
            return bar;
           
        }

        /// <summary>
        /// 菜单管理-修改
        /// create zhl by 2017.3.24
        /// </summary>
        /// <param name="navigation">菜单对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
        public static BaseAnswer UpdateNavigations(Sys_Navigations navigation)
        {
            var sql = "";
            if (navigation.Nav_Title != null)
                sql += "if not exists(select Nav_Title from [Sys_Navigations] where Nav_Title =@Nav_Title ) ";

                sql += "update [Sys_Navigations] set Nav_ParentID=@Nav_ParentID,Nav_Icon=@Nav_Icon,Nav_Linkurl=@Nav_Linkurl,Nav_IsVisible=@Nav_IsVisible,Nav_Level=@Nav_Level  where Nav_Id = @Nav_Id";

            BaseAnswer bar = DapperEdit<T>.DoEdit(sql, navigation);
            return bar;
        }


        /// <summary>
        /// 删除菜单
        /// create zhl by 2017.3.24
        /// </summary>
        /// <param name="id">删除菜单的id</param>
        /// <returns>1正常 返回0</returns>
        public static BaseAnswer DeleteNavigations(int[] id)
        {
            //删除菜单表数据
            var sql = @"delete from [Sys_Navigations]  where  Nav_Id in @Nav_Id";
            //删除权限表对应的菜单数据（未完成）
            BaseAnswer bar = DapperDelete<T>.DoDelete(sql, new { Nav_Id = id.ToArray<int>() });
            return bar;
        }

        /// <summary>
        /// 获取某个角色的权限
        /// </summary>
        /// <param name="rolesId">角色ID</param>
        /// <returns></returns>
        public static List<T> GetNavigationsMenu(int rolesId)
        {
            var sql = "select * from [dbo].[Sys_Jurisdiction] where Roles_Id = @Roles_Id";
            List<T> listMenu = DapperQuery<T>.DoQueryList(sql, new { Roles_Id = rolesId });
            return listMenu;
        }

    }
}
